import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: 祺诺
 * @Name; xqf
 * @Date: 2020/12/23 20:59
 */
@WebServlet("/text2")
public class text2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取文件的mime对象
        // ServletContext  是tomcat在项目中的唯一代表 是一个项目级别的对象该对象一旦创建会一直存在于服务器内存中
        //一个服务器内获取的 ServletContext对象都是同一个
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;chartext=utf-8");
        ServletContext sc = getServletContext();
        String mimeType = sc.getMimeType("xcw.jpg");
        System.out.println(mimeType);
        //获取文件在上传路径中的绝对路径
        String realPath = sc.getRealPath("/xcw.jpg");
        System.out.println(realPath);
        //作为域对象使用 可以做到一定范围数据共享的对象   但是会一直存在于内存中 会占用服务器内存所以使用的少
        sc.setAttribute("stu1","谢钱枫");
        String stu1 = (String) sc.getAttribute("stu1");
        System.out.println(stu1);
        sc.removeAttribute("stu1");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
